from django.shortcuts import render

# Create your views here.
from rest_framework.response import Response

from goods.models import SKU
from utils.pagination import StandardResultsSetPagination

"""
用户选择不同分类，我们返回响应热销的商品，返回２个热销商品
1.接收分类id
2.根据分类id获取商品信息
3.进行序列化操作(将对象转换为字典或json)
4.返回响应


GET   /goods/categories/(?P<category_id>\d+)/hotskus
"""
from rest_framework.views import APIView
from .serializer import HostSKUSerializer
from rest_framework.generics import ListAPIView

# class HotSKUView(APIView):
#     def get(self,request,category_id):
#         # 1.接收分类id
#         # 2.根据分类id获取商品信息
#         skus = SKU.objects.filter(is_launched=True,category_id=category_id).order_by('-sales')[:2]
#         # 3.进行序列化操作(将对象转换为字典或json)
#         serializer = HostSKUSerializer(skus,many=True)
#         # 4.返回响应
#         return Response(serializer.data)

class HotSKUView(ListAPIView):
    pagination_class = None
    serializer_class = HostSKUSerializer
    # queryset = skus = SKU.objects.filter(is_launched=True,category_id=category_id).order_by('-sales')[:2]
    def get_queryset(self):
        category_id = self.kwargs['category_id']

        return SKU.objects.filter(is_launched=True,category_id=category_id).order_by('-sales')[:2]

from rest_framework.filters import OrderingFilter
from rest_framework.pagination import PageNumberPagination

class SKUAPIView(ListAPIView):
    """
    1.根据分类获取所有数据
    2.排序
    3.分类
    """
    # 排序
    filter_backends = [OrderingFilter]
    ordering_fields = ['price','sales','create_time']

    #分页
    pagination_class = StandardResultsSetPagination




    #获取所有数据
    serializer_class = HostSKUSerializer

    def get_queryset(self):
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(is_launched=True, category_id=category_id)




from .serializer import SKUIndexSerializer
from drf_haystack.viewsets import HaystackViewSet

class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer















